package self.hoy.demo.grainmall.product.web;

import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import self.hoy.demo.grainmall.product.entity.Category;
import self.hoy.demo.grainmall.product.service.CategoryService;
import self.hoy.demo.grainmall.product.vo.Catelog2Vo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author hyh
 * @date 2020/7/19
 */
@Controller
//@RequestMapping("/index")
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<Category> categoryList = categoryService.getLevelCategorys(0L);
        model.addAttribute("categorys", categoryList);
        return "index";
    }

    @GetMapping("/index/json/catalog.json")
    @ResponseBody
    public Map<Long, List<Catelog2Vo>> getCatelogJson() {
        Map<Long, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @Autowired
    private RedissonClient redisson;

    @GetMapping("/hello")
    @ResponseBody
    public String hello() throws InterruptedException {
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("");
        RLock lock = redisson.getLock("helloLock");
        lock.lock(10, TimeUnit.SECONDS);
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
//        lock.lock();
        try {
            System.out.println("hello handling ================= ");
            Thread.sleep(5000);
        } finally {
            lock.unlock();
            rLock.unlock();
        }
        return "执行完毕";
    }
}
